package top.gangster.lunan.userservice.controller;


import com.baomidou.mybatisplus.extension.api.R;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.result.Result;
import top.gangster.lunan.userservice.annotation.LoginToken;
import top.gangster.lunan.userservice.annotation.PermissionLimit;
import top.gangster.lunan.userservice.entity.Role;
import top.gangster.lunan.userservice.entity.vo.RoleVo;
import top.gangster.lunan.userservice.service.RoleService;

/**
 * <p>
 * 角色表，角色id不仅映射了该角色的名称，而且代表了该角色所对应的权限等级 前端控制器
 * </p>
 *
 * @author vic
 * @since 2021-11-06
 */
@RestController
@RequestMapping("/userservice/role")
public class RoleController {
  @Autowired
  RoleService roleService;

  /**
   * 列表查看角色
   * @param current 当前页
   * @param size 当前页面大小
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/rolelist")
  public Result getRoleListController(int current,int size){
    RoleVo roleList = roleService.getRoleList(current,size);
    if (roleList == null)
    {
      return Result.error().message("查询失败，未找到任何角色！");
    }
    return Result.ok().data("roleList",roleList);
  }

  /**
   * 通过名字查看角色
   * @param current 当前页
   * @param size 当前页面大小
   * @param roleName 角色名字
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/roleByname")
  public Result getRoleByNameController(int current,int size,String roleName){
    RoleVo roleList = roleService.getRoleByName(current,size,roleName);
    if (roleList == null)
    {
      return Result.error().message("查询失败，未找到任何角色！");
    }
    return Result.ok().data("roleList",roleList);
  }

  /**
   * 添加角色
   * @param role 角色
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PostMapping("/addRole")
  public Result addRoleController(@RequestBody Role role){

    boolean b = roleService.addRole(role);
    if (!b){
      return Result.error().message("新增角色失败！请检查信息输入是否正确！");
    }


    return Result.ok().message("新增角色成功！");
  }

  /**
   * 修改角色
   * @param role 角色
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PutMapping("/modifyRole")
  public Result modifyRoleController(@RequestBody Role role){
    boolean b = roleService.modifyRole(role);
    if (!b){
      return Result.error().message("修改角色失败！请检查信息输入是否正确！");
    }


    return Result.ok().message("修改角色成功！");
  }

  /**
   * 删除角色
   * @param role 角色
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @DeleteMapping("/deleteRole")
  public Result deleteRoleController(@RequestBody Role role){
    boolean b = roleService.deleteRole(role);
    if (!b){
      return Result.error().message("删除角色失败！请检查信息输入是否正确！");
    }
    return Result.ok().message("删除角色成功！");
  }

  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/role")
  public Result getRoleListController(){
    List<Role> roleList = roleService.getRoleList();
    if (roleList == null){
      return Result.error().message("未查找到任何角色！");
    }
    return Result.ok().data("roleList",roleList);
  }

}

